package wuke.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import wuke.dao.KcDao;
import wuke.model.Kcb;
import wuke.model.Zyb;
import wuke.utils.HibernateSessionFactory;

public class KcDaoImp implements KcDao {

	public List getAll() {
		try {
			Session session = HibernateSessionFactory.getSession();
			Transaction ts = session.beginTransaction();
			List list = session.createSQLQuery("from Kcb order by kch").list();
			ts.commit();
			HibernateSessionFactory.closeSession();
			return list;
		} catch (HibernateException e) {

			e.printStackTrace();
			return null;
		}
	}

	public Kcb getOneKc(String kch) {
		try {
			Session session = HibernateSessionFactory.getSession();
			Transaction ts = session.beginTransaction();
			SQLQuery query = session.createSQLQuery("from Kcb where kch=?");
			query.setParameter(0, kch);
			query.setMaxResults(1);
			Kcb kcb = (Kcb) query.uniqueResult();
			ts.commit();
			session.clear();
			HibernateSessionFactory.closeSession();
			return kcb;
		} catch (HibernateException e) {

			e.printStackTrace();
			return null;
		}
	}

}
